package cn.com.connected.base.utils;


import cn.com.connected.base.exception.BusinessException;
import cn.com.connected.base.exception.ErrorCode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class DateUtil {
	public static final SimpleDateFormat FORMAT = new SimpleDateFormat();
	
	public static final String YMD = "yyyyMMdd";
	
	public static final String YMD_HMS = "yyyyMMddHHmmss";
	
	public static final String HMS = "HHmmss";
	
	public static final String HMS_POINT = "HH:mm:ss";
	
	public static final String YMD_SP = "yyyy-MM-dd";
	
	public static final String YMD_HMS_FULL = "yyyy-MM-dd HH:mm:ss";
	
	public static final String Y2MDHMS = "yyMMddHHmmss";
	
	public static final String Y2MDHMS_SSS = "yyMMddHHmmssSSS";
	
	public static final String DEFAULT_REGDOG_TIME = "2015-01-01";
	
	public static final String START = "00:00:00";
	public static final String END = "23:59:59";

	public static  String getShowTime(Date date){
		String showTime;
		long c = (System.currentTimeMillis() - date.getTime())/1000;

		if (c<60){
			showTime = "刚刚";
		}else if(c<60*60){
			showTime = c/60 + "分钟前";
		}else if (c< 24*60*60){
			showTime = c/(60*60) + "小时前";
		}else if (c< 30*24*60*60){
			showTime = c/(24*60*60) + "天前";
		}else{
			showTime = DateUtil.format("yyyy-MM-dd",date);
		}
		return showTime;
	}

	public static String getDbFormat(String time){
		time = time.replaceAll("-","").replaceAll("/","");
		if (time.length()==4){
			return "%Y";
		}else if(time.length()==6){
			return "%Y%m";
		}else if(time.length()==8){
			return "%Y%m%d";
		}else{
			throw new BusinessException(ErrorCode.PARAM_INVALID);
		}
	}

	/**
	 * 给定日期格式，格式化当前日期，并返回
	 * @param pattern
	 * @return
	 */
	public static String format(String pattern){
		return format(pattern,getCurrentDate());
	}

	public static String format(String pattern,Date date){
		FORMAT.applyPattern(pattern);
		return FORMAT.format(date);
	}


	/**
	 * 获取今天最后一秒
	 * @return
	 */
	public static Date getTodayLastTime(){
		return getDate(format("yyyy-MM-dd "+END));
	}

	public static Date getTodayFirstTime(){
		return getDate(format("yyyy-MM-dd "+START));
	}

	/**
	 * 传入日期字符串(2014-12-12),返回日期类型
	 * @param value
	 * @return
	 */
	public static Date getDate(String value){
		try {
			if(value == null) return null;
			
			FORMAT.applyPattern(YMD_SP);
			return FORMAT.parse(value);
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 查询插入日期
	 * @param value
	 * @return
	 */
	public static Date getQueryDate(String value, String prefix){
		try {
			if(value == null) return null;
			
			FORMAT.applyPattern(YMD_HMS_FULL);
			return FORMAT.parse(value+" "+prefix);
		} catch (Exception e) {
			return null;
		}
	}
	
	//查询开始时间
	public static Date getStartDate(String value){
		return getQueryDate(value, START);
	}
	//查询结束时间
	public static Date getEndDate(String value){
		return getQueryDate(value, END);
	}
	
	/**
	 * 通过给定字符串，和给定格式参数，返回日期类型
	 * @param value
	 * @param pattern
	 * @return
	 */
	public static Date getDate(String value, String pattern){
		try {
			FORMAT.applyPattern(pattern);
			return FORMAT.parse(value);
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * 通过日期类型，及格式化字符串，返回日期字符串
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getDate(Date date, String pattern){
		try {
			FORMAT.applyPattern(pattern);
			return FORMAT.format(date);
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 返回当前时间
	 * @return
	 */
	public static Date getCurrentDate(){
		return new Date();
	}
	
	/**
	 * 根据日期生成订单号
	 * @return
	 */
	public static String getOrderCodeFromDate(){
		Date date = new Date();
		FORMAT.applyPattern(Y2MDHMS_SSS);
		return FORMAT.format(date)+(new Random().nextInt(90)+10);
	}


	public static String getNowYMD(){
		Date date = new Date();
		FORMAT.applyPattern(YMD);
		return FORMAT.format(date);
	}
	
	/**
	 * 比较两个日期大小，如果date1大于date2返回true
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compare(Date date1, Date date2){
		if(date2 == null || date2 == null){
			throw new RuntimeException("日期有一个为空");
		}
		long time1 = date1.getTime();
		long time2 = date2.getTime();
		return time1>time2;
	}
	
	/**
	 * 使用默认犬只最后注册日期（老系统），判断日期大小
	 * 	如果传入的日期为空，isValid为false则返回true
	 * @param date
	 * @param isValid  如果为空是否抛异常
	 * @return
	 */
	public static boolean compare(Date date, boolean isValid){
		if(date == null){
			if(isValid){
				throw new RuntimeException("待比较日期为空");
			}
			return true;
		}
		Date defaultDate = getDate(DEFAULT_REGDOG_TIME);
		return compare(date, defaultDate);
	}
	
	public static Date plusDate(Date date, int month){
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MONTH, month);
			return cal.getTime();
		} catch (Exception e) {
			return null;
		}
	}
	
	public static Date plusDay(Date date, int day){
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DAY_OF_YEAR, day);
			return cal.getTime();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 通过给定时间，与当前时间减掉月份，比较大小
	 * @param date
	 * @return
	 */
	public static boolean compare(Date date, int month){
		try {
			Calendar now = Calendar.getInstance();
			now.add(Calendar.MONTH, month);
			long historytime = now.getTime().getTime();
			long nowtime = System.currentTimeMillis();
			long result = (nowtime - historytime) / (60 * 60 * 1000 * 24);
			long result2 = (nowtime - date.getTime()) / (60 * 60 * 1000 * 24);
			return result2 > result;
		} catch (Exception e) {
			return false;
		}
	}
	
	public static boolean compare(Date big, Date small, int month){
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(big);
			cal.add(Calendar.MONTH, month);
			
			long bigtime = cal.getTime().getTime();
			long result = bigtime / (60 * 60 * 1000 * 24);
			long result2 = small.getTime() / (60 * 60 * 1000 * 24);
			return result > result2;
		} catch (Exception e) {
			return false;
		}
	}

//	public static void main(String[] args) {
//		System.out.print(compare(new Date(),DateUtil.getDate("2016-09-28 00:00:00"),-8));
//	}

	/**
	 * 通过给定的日期比较当前时间，是否大于当前时间days天
	 * @param date
	 * @param days
	 * @return
	 */
	public static boolean bigger(Date date, int days){
		Date current = new Date();
		long curtime = current.getTime();
		long time = date.getTime();
		
		return ((curtime-time)/(60 * 60 * 1000 * 24)) >days;
	}
	
	public static boolean bigger(Date big, Date small, int days){
		long bigTime = big.getTime();
		long smallTime = small.getTime();
		
		return (Math.abs(bigTime-smallTime)/(60 * 60 * 1000 * 24)) >days;
	}
}
